<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> 
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %> 
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
 <meta name="renderer" content="webkit">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>项目联系人列表</title>
<jsp:include page="../../import.jsp" flush="true" />
</head>

<script type="text/javascript">
	var prjId='${prjId}'
	var cstId = '${cstId}';
	var detailFlag = '${detailFlag}';
	var grid = null;
	var urgentPrjLinkManGrid=null;
	$(function () {
		var editFlag = true;
		if(detailFlag&&detailFlag=="true"){
			editFlag = false;
			$("#addSelect").hide();
			$("#addBtn").hide();
			$("#removeBtn").hide();
			$("#urgentPrjLinkManAddBtn").hide();
		}
	    initForm();
		initGrid(editFlag);
		initUrgentPrjLinkManGrid();
	});
	
	
	//初始化表单
	function initForm(){
		$("#searchBar").ligerForm();
	}
	
	var lnkTpyList = [];//联系人类型
	var gendList = [];//性别类型
	var certTypeList = [];//证件类型
	//初始化列表
	function initGrid(editFlag){
		var params = {};
		params.prjId = prjId;
		params.cstId = cstId;
		$.ajax({
			type: 'get',
			dataType:'json',
			data:'',
			url: '<%=path%>/project/prjlinkman/getDataList.do',
			success:function(msg){
		 if(msg.success == 1){
			 lnkTpyList = msg.data[0];
			 gendList = msg.data[1];
			 certTypeList = msg.data[2];
		$("#ligergrid").ligerGrid({
	    	url: '<%=path%>/project/prjlinkman/initList.do',
	    	method : 'get',
	    	parms:params,
	        columns: [
	                  { display: '项目角色', name: 'lnkTypCdNm', minWidth: 140 ,
	  	            	editor: {
	                          type: 'select',
	                          data:lnkTpyList,
	                          valueField: 'key',
	      	            	textField: 'value'
	                      },
	                      valueField: 'lnkTypCd',
	                      textField: 'lnkTypCdNm',
	                      render: function (item) {
	                          return item.lnkTypCdNm;
	                      }
	  	            	},
	            { display: '姓名', name: 'lnkNm', minWidth: 60,  editor: { type: 'text' } },
	            { display: '公司职务', name: 'post', minWidth: 140 ,   editor: { type: 'text' }},
	            { display: '性别(*)', name: 'genderCdNm', minWidth: 60 ,
	            	editor: {
                        type: 'select',
                        data:gendList,
                        valueField: 'key',
    	            	textField: 'value'
                    },
	            	},
	            { display: '证件类型(*)', name: 'certTypCdNm', minWidth: 60 ,
	            	editor: {
                        type: 'select',
                        data:certTypeList,
                        valueField: 'key',
    	            	textField: 'value'
                    },
	            	},
	            	   { display: '证件号码(*)', name: 'certNo', width: 140,align:'left' ,  editor: { type: 'text' }},
	   	            { display: '手机号(*)', name: 'mp', width: 100,align:'left' ,   editor: { type: 'text' }},
	   	            { display: '常住地址', name: 'homeAddr', minWidth: 140 ,  editor: { type: 'text' }},
	   	         getOptColumn()
	        ],
            onBeforeSubmitEdit: function (e) {//重新赋值,要不然选中后离开会把key显示在前端
                if (e.selected && e.column.columnname == "certTypCdNm") {
                    e.record.certTypCdId = e.value;
                    e.value = e.selected[e.column.editor.textField];
                    e.record.certTypCd =  e.record.certTypCdId;
                }
                if (e.selected && e.column.columnname == "lnkTypCdNm") {
                    e.record.lnkTypCdId = e.value;
                    e.value = e.selected[e.column.editor.textField];
                    e.record.lnkTypCd = e.record.lnkTypCdId ;
                }
                if (e.selected && e.column.columnname == "genderCdNm") {
                    e.record.genderCdId = e.value;
                    e.value = e.selected[e.column.editor.textField];
                    e.record.genderCd =  e.record.genderCdId;
                }
            },
	        pageSize:100,
	        root:'rows',
	        record:'total',
	        hideLoadButton : true,
	        heightDiff:-2,
	        checkbox:false,
	        enabledEdit: editFlag,
	        allowUnSelectRow: true,
            alternatingRow: false,
	        rownumbers:true,
	        enabledSort: false,
	        usePagerOnLessPagesize: false,
	    });
			    grid = liger.get("ligergrid");
			    //grid.setHeight(200);
		 }else{
					 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
		 }
			}
		});
	}
	
	//获取操作列
	function getOptColumn(){
		var column = {
                display: '操作',
                isSort: false,
                width: 150,
                align:'left',
                render: function (rowdata, rowindex, value){
                	 var h="";
                		if(!detailFlag||detailFlag!="true"){
                        h+='<a class="sq-button-hasicon" href="javascript:removeBtn(';
                        h+="'"+rowdata.id+"'";
                        h+=')"><i class="sqBtnIco_7"></i>删除</a>'
                		}
                    return h;
                }
            }
		return column;
	}
	
	//格式化时间
	function renderDate(rowdata, index, value){
		var dateStr = "";
		if(null != value&&value!=''){
			var date = new Date(value);
			dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DDHHMMSS);
		}
		return dateStr;
	}
	
	function addRow(){
			//添加联系人信息
 			var manager = $("#ligergrid").ligerGetGridManager();
            manager.addRow({
			lnkTypCd:'',
            lnkNm:'',
			post:'',
			genderCd:'',
			certTypCd:'',
			certNo:'',
			mp:'',
			homeAddr:'',
            });
	}
	
	
	function iframeResize () {
	    if (parent.mainApp) {
	        parent.mainApp.parentTab.onResize();
	    };
	}
	function addBtn(){
		addRow();
	}
	//删除所选项目联系人信息
	function removeBtn(id){
	/* 	var selectedRows = grid.getSelectedRows();
		if(selectedRows.length <1){
			$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW, SQ_type: "warring"});
			return;
		} */
		var _ids = [];
		$.ligerDialog.confirm(LogUtils.LOG_CMM_DELETE_CONFIRM, function (flag) {
/* 		for ( var index in selectedRows) {
			if(selectedRows[index].id){
			_ids.push(selectedRows[index].id);
			}else{
				var manager = $("#ligergrid").ligerGetGridManager();
				 manager.deleteSelectedRow();
			}
		} */
		if(flag == false)return;
		if(id&&id!="undefined"&&id!="null"){
				_ids.push(id);
			}else{
				var manager = $("#ligergrid").ligerGetGridManager();
				 manager.deleteSelectedRow();
				 return;
			}
			$.ajax({
				type: 'post',
				dataType:'json',
				data:{ids:JSON.stringify(_ids)},
				url: '<%=path%>/project/prjlinkman/removeByIds.do',
				success:function(msg){
					 if(msg.success == 1){
						 grid.reload();
					 }else{
						 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
					 }
				}
			});
		});
		
	}
	//保存所选项目联系人信息
	function addSelect(){
	/* 	var selectedRows = grid.getSelectedRows();
		if(selectedRows.length <1){
			$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW, SQ_type: "warring"});
			return;
		} */
		
		  var prjlinkManList = grid.getData();
		var params = {};
		params.prjId = prjId;
		params.cstId = cstId;
		params.prjlinkManList = JSON.stringify(prjlinkManList);
	     var wait_dialog;
			$.ajax({
				type: 'post',
				dataType:'json',
				data:params,
				url: '<%=path%>/project/prjlinkman/add.do',
				beforeSend: function(){
			          wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
			     },
				success:function(msg){
					 wait_dialog.close();
					 if(msg.success == 1){
						 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
						 grid.reload();
					 }else{
						 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
					 }
				}
			});
	}
	function addUrgentPrjLinkMan(){
		var cstRlFlag = ProjectConstant.CST_RL_URGENT;//担保人
  		var win =  $.ligerDialog.SQopen({ title: '添加紧急联系人',height: 600,width: 850, url: '<%=path%>/project/prjbscinfo/toChoosePrjCstPage.do?cstRlFlag='+cstRlFlag, isResize: true });
	
/* 		if($("#cstId").val()){
			//添加紧急联系人信息
 			var manager = $("#urgentPrjLinkManGrid").ligerGetGridManager();
            manager.addRow({
			cstId:$("#cstId").val(),
			cstNm:$("#cstNm").val(),
			cstTypCdNm:$("#cstTypCdNm").val(),
			certNo:$("#certNo").val(),
			crtTm:Number($("#crtTm").val()),
            });
		} */
	}
	
	function addSelectUrgentPrjLinkManRow(){
		 		if($("#cstId").val()){
					//添加紧急联系人信息
		 			var manager = $("#urgentPrjLinkManGrid").ligerGetGridManager();
		            manager.addRow({
					cstId:$("#cstId").val(),
					cstNm:$("#cstNm").val(),
					cstTypCdNm:$("#cstTypCdNm").val(),
					certNo:$("#certNo").val(),
					crtTm:Number($("#crtTm").val()),
		            });
				} 
	}
	
	//删除所选项目客户信息
	function removeUrgentPrjLinkMan(id){
	/* 	var selectedRows = grid.getSelectedRows();
		if(selectedRows.length <1){
			$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW, SQ_type: "warring"});
			return;
		} */
		var _ids = [];
		$.ligerDialog.confirm(LogUtils.LOG_CMM_DELETE_CONFIRM, function (flag) {
	/* 	for ( var index in selectedRows) {
			if(selectedRows[index].id){
			_ids.push(selectedRows[index].id);
			}else{
				var manager = $("#ligergrid").ligerGetGridManager();
				 manager.deleteSelectedRow();
			}
		} */
		if(id&&id!="undefined"){
			_ids.push(id);
		}else{
			var manager = $("#urgentPrjLinkManGrid").ligerGetGridManager();
			 manager.deleteSelectedRow();
			 return;
		}
			if(flag == false)return;
			$.ajax({
				type: 'post',
				dataType:'json',
				data:{ids:JSON.stringify(_ids)},
				url: '<%=path%>/project/prjcstbscInfo/removeByIds.do',
				success:function(msg){
					 if(msg.success == 1){
						 urgentPrjLinkManGrid.reload();
					 }else{
						 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
					 }
				}
			});
		});
		
	}
	//紧急联系人初始化列表
	function initUrgentPrjLinkManGrid(){
		var params = {};
		params.prjId = prjId;
		params.cstId = cstId;
		params.cstRlTyp = ProjectConstant.CST_RL_URGENT;
		$("#urgentPrjLinkManGrid").ligerGrid({
			url: '<%=path%>/project/prjcstbscInfo/initList.do',
	    	method : 'get',
	    	parms:params,
	        columns: [
	             { display: '联系人姓名', name: 'cstNm', minWidth: 140 ,
	            	   	render: function (rowdata, rowindex, value, column) { 
		            		var h = '';
		            		    h='<a class="sq-button-hasicon" href="javascript:detailBtn(';
		                        h+="'"+rowdata.cstId+"',";
		                        h+="'"+rowdata.cstTypCd+"'";
		                        h+=')">'+value+'</a>'
		                        return h;
		            		},
	  	            	enabledSort:true},
	            { display: '联系人类型', name: 'cstTypCdNm', minWidth: 100 ,enabledSort:true},
	            { display: '证件/组织机构代码', name: 'certNo', minWidth: 140 , enabledSort:true},
	            { display: '创建时间', name: 'crtTm', minWidth: 140 , enabledSort:true},
	            {
	                display: '操作',
	                isSort: false,
	                width: 100,
	                render: function (rowdata, rowindex, value){
	                    var h='<a class="sq-button-hasicon" href="javascript:removeUrgentPrjLinkMan(';
	                  		  h+="'"+rowdata.id+"'";
	                          h+=')"><i class="sqBtnIco_3"></i>删除</a>'
	                    return h;
	                }
	            }
	        ],
	        pageSize:100,
	        root:'rows',
	        record:'total',
	        hideLoadButton : true,
	        width: '100%',
	        heightDiff:-2,
	        checkbox:true,
	        allowUnSelectRow: true,
            alternatingRow: false,
	        rownumbers:true,
	        usePagerOnLessPagesize: false,
	    });
		urgentPrjLinkManGrid = liger.get("urgentPrjLinkManGrid");
	//	urgentPrjLinkManGrid.setHeight(200);
	}
	
	function detailBtn(id,cstTypCd){
		if(cstTypCd==CustomerConstant.CST_TYPE_NP_CD){
			Add_navTab('tab_cst_detail'+id,'客户详情','<%=path%>/customer/cstbscinfo/toCustCstBacInfoNpDetailPage.do?id='+id);
		}
		if(cstTypCd==CustomerConstant.CST_TYPE_CORP_CD){
			Add_navTab('tab_cst_detail'+id,'客户详情','<%=path%>/customer/cstbscinfo/toCustCstBacInfoCorpDetailPage.do?id='+id);
		}
	}
	
	//保存紧急联系人信息
	function saveSelectUrgentPrjLinkMan(urgentPrjLinkMan){
		//  var prjCstBscInfoList = urgentPrjLinkManGrid.getData();
		  var params = {};
		  params.prjId = prjId;
		  params.cstRlTyp =ProjectConstant.CST_RL_URGENT;   
		  params.prjCstBscInfo = JSON.stringify(urgentPrjLinkMan);
		     var wait_dialog;
			$.ajax({
				type: 'post',
				dataType:'json',
				data:params,
				url: '<%=path%>/project/prjcstbscInfo/addSelectPrjCstBscInfo.do',
				beforeSend: function(){
			          wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
			     },
				success:function(msg){
					 wait_dialog.close();
					 if(msg.success == 1){
						 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
						 urgentPrjLinkManGrid.reload();
					 }else{
						 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
					 }
				}
			});
	}
</script>

<body class="iframeBody">
<div class="Wrapper">
  <div class="pageIn2layout">
<jsp:include page="../../project/prjbscinfo/prjInvestRpt.jsp" flush="true" >
		<jsp:param name="currentNav" value="prjlinkman"/>
</jsp:include>
<div class="pageIn2L_mid">
<div class="searchArea">
    <form id="searchBar">
        <div class="fieldset1 clearFix">
            <div class="clearFix"></div>
            <div class="btnsBox">
               <span id="addBtn" onclick="addBtn();" class="sq-button sq-button-round">新增</span>
           <!--    <span id="removeBtn" onclick="removeBtn();" class="sq-button sq-button-round">作废</span> -->
               <span id="addSelect" class="sq-button sq-button-round" onclick="addSelect()"><!-- <i class="sqBtnIco_10"></i> -->保存</span>
            </div>
        </div>
        </form>
    </div>
            <div style="display: none">
             <input type="hidden"  id="cstId" name="cstId"  class="ui-textbox" value="${cstId}">
            <input type="hidden"  id="prjId" name="prjId"  class="ui-textbox" value="${prjId}">
            <input type="hidden"  id="cstStsCdNm" name="cstStsCdNm"  class="ui-textbox" value="">
            <input type="hidden"  id="cstLvlCdNm" name="cstLvlCdNm"  class="ui-textbox" value="">
            <input type="hidden"  id="cstNm" name="cstNm"  class="ui-textbox" value="">
            <input type="hidden"  id="cstTypCd" name="cstTypCd"  class="ui-textbox" value="">
            <input type="hidden"  id="cstTypCdNm" name="cstTypCdNm"  class="ui-textbox" value="">
            <input type="hidden"  id="certNo" name="certNo"  class="ui-textbox" value="">
            <input type="hidden"  id="crtTm" name="crtTm"  class="ui-textbox" value="">
        </div>
<div id="ligergrid" class="sq-grid"></div>


        <div class="sqPanel">
            <div class="sqPanel_head inL">
                <span>紧急联系人信息</span>
               
            </div>
            <div class="sqPanel_body">
             <div class="btnsBox">
         			<span id="urgentPrjLinkManAddBtn" onclick="addUrgentPrjLinkMan();" class="sq-button sq-button-round"><i class="sqBtnIco_3"></i>新增</span>
  				</div>
             <div id="urgentPrjLinkManGrid" class="sq-grid"></div>
            </div>
  			 </div> 




<!-- 	  <div class="caption">
                                <span class="title">紧急联系人</span>
         </div>
<div class="btnsBox">
         <span id="urgentPrjLinkManAddBtn" onclick="addUrgentPrjLinkMan();" class="sq-button sq-button-round"><i class="sqBtnIco_3"></i>新增</span>
   <span id="addSelect" class="sq-button sq-button-round" onclick="saveSelectUrgentPrjLinkMan()"><i class="sqBtnIco_10"></i>保存</span>  </div>
<div id="urgentPrjLinkManGrid" class="sq-grid"></div>
<div class="btnsBox">
         <span id="addSelect" class="sq-button sq-button-hasicon l-dialog-btn-ok" onclick="addSelect()"><i class="sqBtnIco_10"></i>保存</span>
  </div>
</div> -->
</div>
</div>

</body>
</html>